package model.collision;

import model.Coordinate;
import model.entity.Entity2D;
import model.spawner.Spawner2D;

/**
 * Semplice motore di collisioni che funziona in 2D.
 * @author Max
 *
 */
public class Round2DCollisionEngine extends CollisionEngine {
	
	private Entity2D userShip;
	private Spawner2D spawner;
	
	public Round2DCollisionEngine(Entity2D userShip, Spawner2D spawner) {
		this.userShip = userShip;
		this.spawner = spawner;
	}
	
	
	@Override
	public void checkCollision() {
		
		Entity2D[] mobs = spawner.cloneActalEntityBuffer() ;
		int distance;
		for (int i = 0; i < mobs.length ; i++) {
			
			if(mobs[i] != null ){
				distance = getDistance( userShip.getCoordinate(), mobs[i].getCoordinate() );
				
				if(distance < userShip.getCollisionRay() +mobs[i].getCollisionRay() ) {
					//COLLISION DETECTED!
					System.out.println("Collision detected!");
				}
			}
			
		}
	}
	
	//Calcola la distanza tra due entita'.
	private int getDistance(Coordinate shipCoord, Coordinate mobCoord) {
		int tmp = (shipCoord.getX() - mobCoord.getX()) * (shipCoord.getX() - mobCoord.getX());
		tmp += (shipCoord.getY() - mobCoord.getY()) * (shipCoord.getY() - mobCoord.getY());
		return (int) Math.sqrt(tmp);
	}

}
